package cn.ecpay.unionpay.settle.dao;

import cn.ecpay.unionpay.settle.entity.db.TPvFundSettleSingle;
import cn.ecpay.unionpay.settle.entity.db.TPvQuotaIncrease;
import org.rex.DB;
import org.rex.db.Ps;
import org.rex.db.exception.DBException;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 额度调增
 * Created by zhaoyanzeng on 2018/12/11.
 */
@Repository
public class QuotaIncreaseDao {

    public void insert(TPvQuotaIncrease TPvQuotaIncrease) throws DBException {
        String sql = "INSERT INTO T_PV_QUOTA_INCREASET" +
                "(ID,TXN_TYPE,TXN_NO,ACQ_INS_CODE,TXN_DATE,SND_TIME,CURRENCY_CODE,TXN_AMT,REMARK,RESP_CODE,RESP_MSG,RESP_VALIDATE,CREATE_TIME,IS_NOTIFY," +
                "NOTIFY_TIME,ACS_ACCT_BAL,AVLB_QUOTA_AMT,PRO_ACCT_BAL,ACCT_DATE,NOTIFY_RESP_CODE,NOTIFY_RESP_MSG,NOTIFY_VALIDATE) " +
                "VALUES" +
                "(#{id},#{txnType},#{txnNo},#{acqInsCode},#{txnDate},#{sndTime},#{currencyCode},#{txnAmt},#{remark},#{respCode},#{respMsg},#{respValidate}," +
                "#{createTime},#{isNotify},#{notifyTime},#{acsAcctBal},#{avlbQuotaAmt},#{proAcctBal},#{acctDate},#{notifyRespCode},#{notifyRespMsg},#{notifyValidate})";
        DB.update(sql, TPvQuotaIncrease);
    }

    public void updateNotify(TPvQuotaIncrease TPvQuotaIncrease) throws DBException {
        String sql = "UPDATE T_PV_QUOTA_INCREASET SET NOTIFY_RESP_CODE=#{notifyRespCode},NOTIFY_RESP_MSG=#{notifyRespMsg}," +
                "IS_NOTIFY=#{isNotify},NOTIFY_TIME=#{notifyTime},NOTIFY_VALIDATE=#{notifyValidate},acs_Acct_Bal=#{acsAcctBal}," +
                "acct_Date=#{acctDate},pro_Acct_Bal=#{proAcctBal},avlb_Quota_Amt=#{avlbQuotaAmt} " +
                "WHERE TXN_NO=#{txnNo}";
        DB.update(sql, TPvQuotaIncrease);
    }

    /**
     * 根据ID获取交易信息
     *
     * @param id
     * @return
     * @throws DBException
     */
    public TPvQuotaIncrease findById(String id) throws DBException {
        String sql = "SELECT * FROM T_PV_QUOTA_INCREASET WHERE ID=?";
        return DB.get(sql, new Object[]{id}, TPvQuotaIncrease.class);
    }

    /**
     * 根据流水号获取交易信息
     *
     * @param txnNo
     * @return
     * @throws DBException
     */
    public TPvQuotaIncrease findByTxnNo(String txnNo) throws DBException {
        String sql = "SELECT * FROM T_PV_QUOTA_INCREASET WHERE TXN_NO=?";
        return DB.get(sql, new Object[]{txnNo}, TPvQuotaIncrease.class);
    }

    /**
     * 当天交易流水号不能重复
     * @param txnNo
     * @param txnDate
     * @return
     * @throws DBException
     */
    public boolean isExistTxn(String txnNo, String txnDate) throws DBException {
        Ps ps = new Ps();
        String sql = "SELECT * FROM T_PV_QUOTA_INCREASET WHERE TXN_NO=? and TXN_DATE =?";
        ps.add(txnNo);
        ps.add(txnDate);
        List<TPvFundSettleSingle> list = DB.getList(sql,ps,TPvFundSettleSingle.class);
        if(list!= null && list.size()>0){
            return true;
        }
        return false;
    }

}
